rm(list = ls())
gc()

library(data.table)
library(dplyr)
library(stringr)
library(tidyr)
library(gender)
library(lfe)


surv=fread('survey-analysis.csv.gz')
surv[countyfips=='',countyfips:=NA]
surv[ZipCode=='',ZipCode:=NA]

surv[,Race:=factor(Race,levels=c('Other','Asian','Black','Hispanic','White'))]

# Perceptions of neighborhoods


m = felm(NeighborDems~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)
m1DemSpExp = summary(m,robust=T)
m1DemSpExp$mean.outcome=mean(m$response)


m = felm(NeighborDems~ RepSpExp_nohh_alt  +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)
m1RepSpExp = summary(m,robust=T)
m1RepSpExp$mean.outcome=mean(m$response)


#### Contact with neighbor dems

m = felm(ContactDemNeighbors~ DemSpExp_nohh_alt +hh.d.adj_alt+hh.n.adj_alt  + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)
m2DemSpExp = summary(m,robust=T)
m2DemSpExp$mean.outcome=mean(m$response)

m = felm(ContactRepNeighbors~ RepSpExp_nohh_alt +hh.r.adj_alt+hh.n.adj_alt + WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7 |ZipCode|0|countyfips,data=surv)
m2RepSpExp = summary(m,robust=T)
m2RepSpExp$mean.outcome=mean(m$response)


# Comfort
m = felm(Comfort~(DemSpExp_nohh_alt + hh.d.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Democrat |ZipCode|0|countyfips,data=surv)
m5DemSpExp = summary(m,robust=T)
m5DemSpExp$mean.outcome=mean(m$response)

m = felm(Comfort~(RepSpExp_nohh_alt  + hh.r.adj_alt+hh.n.adj_alt+ WhiteBlockGroup + AgeBlockGroup + RegsBlockGroup + HHIncomeBlockGroup  + CollegeBlockGroup + HomeownerBlockGroup + YearBuiltBlockGroup +DriveWorkBlockGroup + EmplBlockGroup + HouseValueBlockGroup + Race + Age + Male + Liberalism + Married  + College + Homeowner + YearsResidence+Party7)*Republican |ZipCode|0|countyfips,data=surv)
m5RepSpExp = summary(m,robust=T)
m5RepSpExp$mean.outcome=mean(m$response)



# Dem models
dems = surv[Party_year1=='Democrat']
reps = surv[Party_year1=='Republican']
oths = surv[Party_year1=='Other']




m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)
m6DemSpExpDems = summary(m,robust=T)
m6DemSpExpDems$mean.outcome=(m$response)


m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)
m6DemSpExpReps = summary(m,robust=T)
m6DemSpExpReps$mean.outcome=(m$response)

m = felm(Democrat~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)
m6DemSpExpOths = summary(m,robust=T)
m6DemSpExpOths$mean.outcome=(m$response)



m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=dems)
m6RepSpExpDems = summary(m,robust=T)
m6RepSpExpDems$mean.outcome=(m$response)


m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=reps)
m6RepSpExpReps = summary(m,robust=T)
m6RepSpExpReps$mean.outcome=(m$response)

m = felm(Republican~ Comfort + Race + Age + Male  + Married  + College + Homeowner + YearsResidence |ZipCode|0|0,data=oths)
m6RepSpExpOths = summary(m,robust=T)
m6RepSpExpOths$mean.outcome=(m$response)


##
rm(surv,w,data,dems,reps,oths,m,t)
save.image(file='results/survey-results-no-weights-alt.Rdata')